package kokonut.shop.cart;

import java.io.Serializable;
import java.util.ArrayList;

import kokonut.shop.product.Product;
import kokonut.shop.product.TShirt;


public class Cart implements Serializable {

	private static Cart INSTANCE = new Cart();

    private Cart() {}

    public static Cart getInstance() {
        return INSTANCE;
    }

	private ArrayList<SelectedProduct> productList;

	public ArrayList<SelectedProduct> getProductList()
	{
		return this.productList;
	}

	/**
	 * Calculates the total price of all the products in the cart
	 * @return Total price in float format
	 */
	public float getTotal()
	{
		if(this.productList == null)
			return 0;
		else
		{
			float total = 0;
			for(SelectedProduct p : productList)
				total += p.getSubtotal();
			return total;
		}
	}

	/**
	 * Adds a product to the cart
	 * @param product: The product to be added to the cart
	 */
	public void addProduct(TShirt shirt)
	{
		if(this.productList == null)
			this.productList = new ArrayList<SelectedProduct>();
		this.productList.add(new SelectedProduct(shirt,1));
	}

	/**
	 * Adds a product to the cart specifying the amount
	 * @param product: The product to be added to the cart
	 */
	public void addProduct(TShirt shirt, int amount)
	{
		if(this.productList == null)
			this.productList = new ArrayList<SelectedProduct>();
		this.productList.add(new SelectedProduct(shirt,amount));
	}

	/**
	 * Removes a product from the cart
	 * @param product: The product to be removed
	 */
	public void removeProduct(Product product)
	{
		if(this.productList != null && !this.productList.isEmpty())
			this.productList.remove(product);
	}
}